# -*- coding: utf-8 -*-

from PyQt5 import QtCore, QtGui, QtWidgets

from PyQt5.QtCore import QRegExp, Qt
from PyQt5.QtGui import QRegExpValidator


def isEmpty(obj):
    if obj is None or str.strip(obj) == "":
        return True


class Ui_Dialog(object):

    def setupUi(self, Dialog):
        self.id = id
        Dialog.setObjectName("Dialog")
        Dialog.resize(271, 273)
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap("ui/logo.svg"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        Dialog.setWindowIcon(icon)
        Dialog.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint)
        self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label.sizePolicy().hasHeightForWidth())
        self.label.setSizePolicy(sizePolicy)
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.le_Name = QtWidgets.QLineEdit(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.le_Name.sizePolicy().hasHeightForWidth())
        self.le_Name.setSizePolicy(sizePolicy)
        self.le_Name.setObjectName("le_Name")
        # 判断是否全部都已经输入完了
        self.le_Name.textChanged.connect(self.changeSaveButton)
        self.horizontalLayout.addWidget(self.le_Name)
        self.verticalLayout.addLayout(self.horizontalLayout)

        self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_7.setObjectName("horizontalLayout_7")
        self.label_6 = QtWidgets.QLabel(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.label_6.sizePolicy().hasHeightForWidth())
        self.label_6.setSizePolicy(sizePolicy)
        self.label_6.setObjectName("label_6")
        self.horizontalLayout_7.addWidget(self.label_6)
        self.le_Datasource = QtWidgets.QLineEdit(Dialog)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.le_Datasource.sizePolicy().hasHeightForWidth())
        self.le_Datasource.setSizePolicy(sizePolicy)
        self.le_Datasource.setObjectName("le_Datasource")
        # 判断是否全部都已经输入完了
        self.le_Datasource.textChanged.connect(self.changeSaveButton)
        self.horizontalLayout_7.addWidget(self.le_Datasource)
        self.verticalLayout.addLayout(self.horizontalLayout_7)

        self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_6.setObjectName("horizontalLayout_6")
        self.label_5 = QtWidgets.QLabel(Dialog)
        self.label_5.setObjectName("label_5")
        self.horizontalLayout_6.addWidget(self.label_5)
        self.le_IP = QtWidgets.QLineEdit(Dialog)
        self.le_IP.setObjectName("le_IP")
        # 判断是否全部都已经输入完了
        self.le_IP.textChanged.connect(self.changeSaveButton)
        self.horizontalLayout_6.addWidget(self.le_IP)
        self.verticalLayout.addLayout(self.horizontalLayout_6)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.le_PORT = QtWidgets.QLineEdit(Dialog)
        # 判断是否全部都已经输入完了
        self.le_PORT.textChanged.connect(self.changeSaveButton)
        self.le_PORT.setInputMethodHints(QtCore.Qt.ImhDigitsOnly)
        self.le_PORT.setObjectName("le_PORT")
        reg = QRegExp('[0-9]+$')
        validator = QRegExpValidator()
        validator.setRegExp(reg)
        self.le_PORT.setValidator(validator)
        self.horizontalLayout_2.addWidget(self.le_PORT)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_3 = QtWidgets.QLabel(Dialog)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_3.addWidget(self.label_3)
        self.le_USERNAME = QtWidgets.QLineEdit(Dialog)
        self.le_USERNAME.setObjectName("le_USERNAME")
        # 判断是否全部都已经输入完了
        self.le_USERNAME.textChanged.connect(self.changeSaveButton)
        self.horizontalLayout_3.addWidget(self.le_USERNAME)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.label_4 = QtWidgets.QLabel(Dialog)
        self.label_4.setObjectName("label_4")
        self.horizontalLayout_4.addWidget(self.label_4)
        self.le_PASSWORD = QtWidgets.QLineEdit(Dialog)
        self.le_PASSWORD.setObjectName("le_PASSWORD")
        # 判断是否全部都已经输入完了
        self.le_PASSWORD.textChanged.connect(self.changeSaveButton)
        self.horizontalLayout_4.addWidget(self.le_PASSWORD)
        self.verticalLayout.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.cancelButton = QtWidgets.QPushButton(Dialog)
        self.cancelButton.setObjectName("cancelButton")
        self.cancelButton.setFocusPolicy(Qt.NoFocus)
        self.horizontalLayout_5.addWidget(self.cancelButton)
        self.saveButton = QtWidgets.QPushButton(Dialog)
        self.saveButton.setObjectName("saveButton")
        self.saveButton.setDisabled(True)
        self.saveButton.setFocusPolicy(Qt.StrongFocus)
        self.horizontalLayout_5.addWidget(self.saveButton)
        self.verticalLayout.addLayout(self.horizontalLayout_5)

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "添加设计文档来源"))
        self.label.setText(_translate("Dialog", "名  称："))
        self.label_6.setText(_translate("Dialog", "数据库："))
        self.label_5.setText(_translate("Dialog", "ip地址："))
        self.label_2.setText(_translate("Dialog", "端  口："))
        self.label_3.setText(_translate("Dialog", "用户名："))
        self.label_4.setText(_translate("Dialog", "密  码："))
        self.cancelButton.setText(_translate("Dialog", "取消"))
        self.saveButton.setText(_translate("Dialog", "保存"))
        self.saveButton.setToolTip("信息必须全部填写完毕才可保存~")

    def changeSaveButton(self):
        key = False
        name = self.le_Name.text()
        ip = self.le_IP.text()
        username = self.le_USERNAME.text()
        port = self.le_PORT.text()
        password = self.le_PASSWORD.text()
        datasource = self.le_Datasource.text()
        if isEmpty(name) or isEmpty(ip) or isEmpty(username) or isEmpty(port) or isEmpty(password) or isEmpty(
                datasource):
            key = True
        self.saveButton.setDisabled(key)
